#include <iostream>
#include <vector>
#include <climits>
#include <cmath>
using namespace std;

void getFac(int x, vector<int>& v)
{
    for (int i = 2; i <= sqrt(x); ++i)
    {
        if (x % i == 0)
        {
            v.push_back(i);
            if (x / i != i)
                v.push_back(x / i);
        }
    }
}
int jump(int n, int m)
{
    vector<int> step(m + 1, INT_MAX);
    step[n] = 0;
    for (int i = n; i <= m; ++i)
    {
        if (step[i] == INT_MAX)
            continue;

        vector<int> fac;
        getFac(i, fac);
        for (int j = 0; j < fac.size(); ++j)
        {
            if (i + fac[j] <= m)
                step[i + fac[j]] = min(step[i + fac[j]], step[i] + 1);
        }
    }
    return step[m] == INT_MAX ? -1 : step[m];
}
int main() {
    int n, m;
    cin >> n >> m;
    cout << jump(n, m) << endl;
    return 0;
}